<?php
declare(strict_types=1);

namespace plugin\ky_black\app\admin\validate;

use think\Validate;

class OrganizationValidate extends Validate
{
    protected $rule = [
        'company_name' => 'require|max:100',
        'business_license' => 'require|max:50',
        'legal_representative' => 'require|max:30',
        'company_address' => 'require|max:255',
        'contact_phone' => 'require|regex:/^1[3-9]\d{9}$/',
        'company_email' => 'email|max:100',
        'invoice_type' => 'require|in:normal,special',
        'tax_number' => 'require|max:50',
        'bank_name' => 'require|max:100',
        'bank_account' => 'require|max:50',
        'invoice_address' => 'require|max:255',
        'invoice_phone' => 'require|regex:/^1[3-9]\d{9}$/',
        'contact_name' => 'require|max:30',
        'contact_mobile' => 'require|regex:/^1[3-9]\d{9}$/',
        'black_card_no' => 'max:50',
        'status' => 'in:0,1'
    ];

    protected $message = [
        'company_name.require' => '企业名称不能为空',
        'company_name.max' => '企业名称最多100个字符',
        'business_license.require' => '营业执照号不能为空',
        'business_license.max' => '营业执照号最多50个字符',
        'legal_representative.require' => '法定代表人不能为空',
        'legal_representative.max' => '法定代表人最多30个字符',
        'company_address.require' => '企业地址不能为空',
        'company_address.max' => '企业地址最多255个字符',
        'contact_phone.require' => '联系电话不能为空',
        'contact_phone.regex' => '联系电话格式不正确',
        'company_email.email' => '企业邮箱格式不正确',
        'company_email.max' => '企业邮箱最多100个字符',
        'invoice_type.require' => '发票类型不能为空',
        'invoice_type.in' => '发票类型不正确',
        'tax_number.require' => '税号不能为空',
        'tax_number.max' => '税号最多50个字符',
        'bank_name.require' => '开户行不能为空',
        'bank_name.max' => '开户行最多100个字符',
        'bank_account.require' => '银行账号不能为空',
        'bank_account.max' => '银行账号最多50个字符',
        'invoice_address.require' => '开票地址不能为空',
        'invoice_address.max' => '开票地址最多255个字符',
        'invoice_phone.require' => '开票电话不能为空',
        'invoice_phone.regex' => '开票电话格式不正确',
        'contact_name.require' => '联系人不能为空',
        'contact_name.max' => '联系人最多30个字符',
        'contact_mobile.require' => '联系人手机不能为空',
        'contact_mobile.regex' => '联系人手机格式不正确',
        'black_card_no.max' => '黑卡卡号最多50个字符',
        'status.in' => '状态值不正确'
    ];

    protected $scene = [
        'create' => ['company_name', 'business_license', 'legal_representative', 'company_address', 'contact_phone', 
                    'company_email', 'invoice_type', 'tax_number', 'bank_name', 'bank_account', 'invoice_address', 
                    'invoice_phone', 'contact_name', 'contact_mobile', 'black_card_no', 'status'],
        'edit' => ['company_name', 'business_license', 'legal_representative', 'company_address', 'contact_phone', 
                  'company_email', 'invoice_type', 'tax_number', 'bank_name', 'bank_account', 'invoice_address', 
                  'invoice_phone', 'contact_name', 'contact_mobile', 'black_card_no', 'status']
    ];

    /**
     * 自定义唯一性检查
     */
    protected function checkUnique($value, $rule, $data = [])
    {
        list($table, $field) = explode(',', $rule);
        
        // 添加调试日志
        error_log("Checking unique for value: " . $value);
        error_log("Table: " . $table);
        error_log("Field: " . $field);
        
        // 构建查询
        $query = \support\Db::table($table)->where($field, $value);
        
        // 如果是编辑操作，排除当前记录
        if (!empty($data['org_id'])) {
            $query->where('org_id', '<>', $data['org_id']);
        }
        
        // 执行查询并记录SQL
        $sql = $query->toSql();
        error_log("SQL Query: " . $sql);
        
        $exists = $query->exists();
        error_log("Result exists: " . ($exists ? 'true' : 'false'));
        
        return !$exists || '该记录已存在';
    }
} 